package com.ithuameng.admin.app.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithuameng.admin.app.system.model.SysJob;
import com.ithuameng.admin.app.system.model.SysJobLog;
import com.ithuameng.admin.app.system.model.admin.dto.SysJobDto;
import com.ithuameng.admin.app.system.model.admin.qo.QuartzJobQo;
import com.ithuameng.admin.app.system.model.admin.vo.QuartzJobRunVo;
import com.ithuameng.admin.app.system.model.admin.vo.QuartzJobVo;
import com.ithuameng.admin.app.system.model.vo.AdminPageResVo;
import com.ithuameng.admin.app.system.model.vo.R;
import com.ithuameng.admin.app.system.service.ISysJobLogService;
import com.ithuameng.admin.app.system.service.ISysJobService;
import com.ithuameng.admin.enums.CommonError;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 定时任务控制器
 *
 * @author ithuameng
 */
@Api(tags = "定时任务控制器")
@RestController
@RequestMapping("/admin/v1/quartz/job")
public class QuartzJobController {

    @Resource
    private ISysJobService quartzJobService;

    @Resource
    private ISysJobLogService quartzJobLogService;

    @ApiOperation(value = "分页列表查询")
    @PreAuthorize("@ss.hasPermi('system:job:list')")
    @GetMapping(value = "/list")
    public R<AdminPageResVo<QuartzJobVo>> queryPageList(QuartzJobQo quartzJobQo) {
        SysJobDto quartzJobDto = new SysJobDto();
        BeanUtils.copyProperties(quartzJobQo, quartzJobDto);
        Page<SysJob> page = new Page<>(quartzJobQo.getPageNumber(), quartzJobQo.getPageSize());
        IPage<SysJob> pageList = quartzJobService.search(page, quartzJobDto);
        AdminPageResVo<QuartzJobVo> quartzJobPage = new AdminPageResVo<>();
        List<QuartzJobVo> qjs = new ArrayList<>();
        QuartzJobVo qj = null;
        for (SysJob job : pageList.getRecords()) {
            qj = new QuartzJobVo();
            BeanUtils.copyProperties(job, qj);
            qjs.add(qj);
        }
        quartzJobPage.setRecords(qjs);
        quartzJobPage.setPage(pageList.getPages());
        quartzJobPage.setTotal(pageList.getTotal());
        return new R<>(quartzJobPage);
    }

    @ApiOperation(value = "查询定时任务详情")
    @GetMapping(value = "/detail")
    public R<SysJob> queryById(@RequestParam(name = "id") String id) {
        SysJob quartzJob = quartzJobService.getById(id);
        if (quartzJob == null) {
            return new R<>(CommonError.INTERNAL_SERVER_ERROR, "未找到相关定时任务");
        }
        return new R<>(quartzJob);
    }

    @ApiOperation(value = "添加定时任务")
    @PreAuthorize("@ss.hasPermi('system:job:add')")
    @PostMapping(value = "/add")
    public R<Void> add(@RequestBody SysJob quartzJob) {
        boolean isSuccess = quartzJobService.saveAndScheduleJob(quartzJob);
        if (isSuccess) {
            return new R<>(CommonError.OK, "创建定时任务成功");
        }
        return new R<>(CommonError.INTERNAL_SERVER_ERROR, "创建定时任务失败");
    }

    @ApiOperation(value = "更新定时任务")
    @PreAuthorize("@ss.hasPermi('system:job:edit')")
    @PostMapping(value = "/update")
    public R<Void> eidt(@RequestBody SysJob quartzJob) {
        boolean isSuccess = quartzJobService.editAndScheduleJob(quartzJob);
        if (isSuccess) {
            return new R<>(CommonError.OK, "更新定时任务成功");
        }
        return new R<>(CommonError.INTERNAL_SERVER_ERROR, "更新定时任务失败!");
    }

    @ApiOperation(value = "执行定时任务")
    @PreAuthorize("@ss.hasPermi('system:job:run')")
    @PostMapping(value = "/run")
    public R<Void> run(@RequestBody QuartzJobRunVo quartzJobRunVo) {
        boolean isSuccess = quartzJobService.run(quartzJobRunVo.getId(), quartzJobRunVo.getParameter());
        if (isSuccess) {
            return new R<>(CommonError.OK, "执行定时任务成功");
        }
        return new R<>(CommonError.INTERNAL_SERVER_ERROR, "执行定时任务失败");
    }

    @ApiOperation(value = "删除定时任务")
    @PreAuthorize("@ss.hasPermi('system:job:remove')")
    @DeleteMapping(value = "/delete")
    public R<Void> delete(@RequestParam(name = "id") String id) {
        boolean isSuccess = quartzJobService.deleteAndStopJob(id);
        if (isSuccess) {
            return new R<>(CommonError.OK, "删除成功!");
        }
        return new R<>(CommonError.INTERNAL_SERVER_ERROR, "删除失败!");
    }

    @ApiOperation(value = "批量删除定时任务")
    @DeleteMapping(value = "/deleteBatch")
    public R<Void> deleteBatch(@RequestParam(name = "ids") String ids) {
        if (ids == null || "".equals(ids.trim())) {
            return new R<>(CommonError.INTERNAL_SERVER_ERROR, "参数不识别！");
        }
        quartzJobService.deleteAndStopJobBatch(ids.split(","));
        return new R<>(CommonError.OK, "删除定时任务成功!");
    }

    @ApiOperation(value = "暂停定时任务")
    @PreAuthorize("@ss.hasPermi('system:job:pause')")
    @GetMapping(value = "/pause")
    public R<Void> pauseJob(@RequestParam(name = "id") String id) {
        boolean isSuccess = quartzJobService.pauseJob(id);
        if (isSuccess) {
            return new R<>(CommonError.OK, "暂停定时任务成功");
        }
        return new R<>(CommonError.INTERNAL_SERVER_ERROR, "暂停定时任务失败!");
    }

    @ApiOperation(value = "恢复定时任务")
    @PreAuthorize("@ss.hasPermi('system:job:resume')")
    @GetMapping(value = "/resume")
    public R<Void> resumeJob(@RequestParam(name = "id") String id) {
        boolean result = quartzJobService.resumeJob(id);
        if (!result) {
            return new R<>(CommonError.INTERNAL_SERVER_ERROR, "定时任务不存在！");
        }
        return new R<>(CommonError.OK, "恢复定时任务成功");
    }

    @ApiOperation(value = "查询运行日志")
    @GetMapping(value = "/log")
    public R<List<SysJobLog>> getJobLog(@RequestParam(name = "id") String id) {
        return new R<>(quartzJobLogService.getJobLogByJobId(id));
    }
}
